{extend name="layout/iframe" /}

{block name="content"}
<blockquote class="layui-elem-quote quoteBox">
    <form class="layui-form h between">
        <div class="layui-inline searchBox">
            <div class="layui-input-inline">
                <input type="text" class="layui-input name" name="name" placeholder="请输入搜索的内容" />
            </div>
            <a class="layui-btn search_btn" data-type="reload">搜索</a>
            <button type="reset" class="layui-btn layui-btn-danger reset_btn"><i class="layui-icon">&#xe669;</i></button>
        </div>
        <!--<div class="layui-inline">
            <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
        </div>-->
        <div class="layui-inline">
            <!--<a href="{:url('admin/product/add')}" class="layui-btn layui-btn-normal add_btn">新增</a>-->
            <a href="javascript:;"  class="layui-btn layui-btn-normal selection"><i class="layui-icon"></i> 确定</a>
        </div>
    </form>
</blockquote>
<input type="hidden" id="supid" value="{$supid}">
<table id="dataTable01" lay-filter="dataTable"></table>

<input type="hidden" class="get_supproduct_url" value="{:url('admin/Purchase/get_supproduct')}">
<input type="hidden" class="save_caigou_session_url" value="{:url('admin/Purchase/save_caigou_session')}">
{/block}

{block name="pagescript"}

    <script type="text/javascript">
        layui.use(['form', 'layer', 'table'], function(){
            var form = layui.form,
                layer = parent.layer === undefined ? layui.layer : top.layer,
                $ = layui.jquery,
                table = layui.table;
            var get_supproduct_url = $('.get_supproduct_url').val();
            var save_caigou_session_url = $('.save_caigou_session_url').val();
            // Table 列表
            var supProduct = table.render({
                elem: '#dataTable01',
                url : get_supproduct_url,
                page : true,
                where: {
                    supid: $('#supid').val()
                },
                cellMinWidth : 95,
                limit : 50,
                limits : [10, 20, 50, 100],
                id : "tableList",
                // initSort: {
                //     field: 'create_date' // 排序字段，对应 cols 设定的各字段名
                //     ,type: 'desc' // 排序方式  asc: 升序、desc: 降序、null: 默认排序
                // },
                cols : [[
                    {type: "checkbox", fixed:"left", width:50},
                    {field: 'name', title: '商品名称', align:'center'},
                    {field: 'img_url', title: '封面图', minWidth:80, align:"center", templet:function(data){
                        return '<a href="'+data.img_url+'" target="_blank"><img src="'+data.img_url+'" style="width: 80px;" /></a>';
                    }},
                    {field: 'bar_code', title: '条码', align:'center'},
                    {field: 'marketable', title: '现有库存', align:'center'},
                    {field: 'cname', title: '分类', align:'center', sort: true},
                    {field: 'unit', title: '单位', align:'center',templet:function(data){
                        return show_product_unit(data.unit);
                    }},


                    /*{title: '操作', minWidth:175, templet:'#handleBar', fixed:"right", align:"center"},*/

                ]]
            });

            // 搜索
            $(".search_btn").on("click", function(){
                if($('.name').val() != ''){
                    table.reload("tableList", {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        },
                        where: {
                            name: $('.searchBox').find('input[name=name]').val()  // 搜索的关键字
                        }
                    });
                }else{
                    layer.msg("请输入搜索的内容");
                }
            });

            // 清空搜索
            $(".reset_btn").on("click", function(){
                table.reload("tableList", {
                    where: {
                        name: ''
                    }
                });
            });

            $('.selection').on('click',function () {
                var checkStatus = table.checkStatus('tableList'),
                    data = checkStatus.data;
                var temp = [];
                if(data.length > 0) {
                    for (var i = 0;i<data.length;i++) {
                        var datas = {
                            id: data[i].id,
                            name:data[i].name,
                            purchase_price:data[i].purchase_price,
                            bar_code:data[i].bar_code,
                            store_count:data[i].marketable,
                            caigou_num:1,
                            total_price:data[i].purchase_price,
                            unit:data[i].unit,
                            spec:data[i].spec,
                            supid:data[i].supplier_id
                        };
                        temp.push(datas)

                    }
                    var data = JSON.stringify(temp)
                    $.ajax({
                        url: save_caigou_session_url,
                        data: {data},
                        type: "POST",
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 1) {
                                layer.closeAll();
                            } else {
                                layer.msg(res.msg);
                            }
                        },
                        error: function (data) {
                            layer.msg("服务器无响应");
                        }
                    });
                }else {
                    layer.msg("至少选择一件商品");
                }


            })

        })
    </script>


{/block}